<style lang="scss">

</style>

<template>

  <div class="z-field-set" :style="zFieldsetStyle" >
<!--    {{zFieldsetStyle}}-->
    <!--  data arr {{binds.config}} {{widgetName}} {{inited}}-->
<!--    {{runStr}}-->
<!--    {{widgetConfig}}-->
    <slot></slot>
  </div>
</template>

<script>
import {evalStr} from "@/zform/evalGlo";

const PREFIX = 'z-fieldset__';

export default {
  name: "ZFieldset",
  inject: [
    'curFormCon'
  ],
  props: {
    binds: {
      type: Object,
      default() {
        return {
          config: {}
        }
      }
    },

  },

  data() {
    let self = this
    let config = this?.binds?.config?.ui?.wrap_start_config ?? {}
    // console.log(config)
    let widgetConfig = config?.ui?.widgetConfig;
    let link = widgetConfig.link
    let linkObj = null
    let runStr = ''
    try {
      let o = ZY.JSON5.parse(widgetConfig.link)
      let f = o.data.label
      try {
        linkObj = ZY.JSON5.parse(ZY.JSON5.parse(f));
        // console.log(linkObj)
        runStr = linkObj?.textContent
      } catch (e) {
        console.log('parse fieldset link err', e)
      }
    } catch (e) {
      console.log('parse fieldset link err', e)
    }
    return {
      // inited: false,
      config,
      widgetConfig,
      runStr,
      linkObj,
      isShowed: false,
      // isCustomRender: this.binds.config.ui.isCustomRender,
      widgetName:  PREFIX + ZY.rid(6),
      isLocked: false,
    }
  },
  computed: {
    zFieldsetStyle() {
      // console.log('zFieldsetStyle')
      let ret = {
      }
      if (!this.isLocked && this.widgetConfig.defaultShow) {

        // delete ret.display;
        return ret;
      }
      if (!this.isShowed) {
        ret.display = 'none'
      }
      return ret
    },
  },
  created() {
    // this.reload()
    this.curFormCon.registerHook('afterUpdate', this.widgetName, this.private_render)
    // console.log('data-arr', this.tableConfig)
  },
  beforeUnMount() {
    this.curFormCon.unRegisterHook('afterUpdate', this.widgetName)
  },
  methods: {

    reload() {
      this.inited = false
      this.private_render();
      // this.$nextTick(() => {
      //   this.inited = true
      // })
      this.inited = true
    },
    private_render ({partName, model, s_path, getCmFieldContext, cmFields, e}) {
      // console.log(partName, model, s_path, getCmFieldContext, cmFields, e)

      function M(pathArr = []) {
        let s_path = ZY.getObjPathFromPathArr(pathArr);
        if (s_path) {
          return ZY.lodash.get(model, s_path)
        }
      }

      function SELECT_LABEL(pathArr = [], label) {
        // console.log(pathArr, label)
        let s_path = ZY.getObjPathFromPathArr(pathArr);
        if (s_path) {
          // console.log(s_path)
          let ctx = getCmFieldContext(s_path)
          if (ctx) {
            // console.log(ctx)
            if (ctx.getOptions) {
              let option = ctx.getCurOption();

              // console.log(option)
              if (option) {
                return option.label === label
              }
            }
          }
          // return ZY.lodash.get(model, s_path)
        }
      }

       // console.log(cmFields)

      let ret = evalStr(this.runStr, {
        M,
        SELECT_LABEL,
        EQ: function (a, b) {
          return a == b
        }
      });

      this.isLocked = true;

      // console.log(ret)
      this.isShowed = Boolean(ret)
    },
  }
}
</script>


